package com.bu.utils

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import bu.utils.SPUtil
import kotlinx.android.synthetic.main.activity_sp.*

class SPActivity : AppCompatActivity(), View.OnClickListener {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sp)
        SPUtil.init(this)
        initView()
    }

    private fun initView() {
        sp_add.setOnClickListener(this)
        sp_remove.setOnClickListener(this)
        sp_getall.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        var fileName: String = if (TextUtils.isEmpty(sp_file_name.text.toString().trim())) sp_file_name.text.toString().trim() else "default_sp"
        val key = sp_key.text.toString().trim()
        val value = sp_value.text.toString()
        when (v?.id) {
            R.id.sp_add -> {
                if (!TextUtils.isEmpty(key) && !TextUtils.isEmpty(value)){
                    SPUtil.put(fileName,key,value)
                }
            }
            R.id.sp_remove -> {
                if (!TextUtils.isEmpty(key)){
                    SPUtil.remove(fileName,key)
                }
            }
            R.id.sp_getall -> {
                sp_text.text = SPUtil.getAll(fileName).toString()
            }
        }
    }
}
